package org.jackysoft.ant.taskdef;

import java.io.File;
import java.io.FileOutputStream;
import java.util.Properties;

import org.apache.tools.ant.Task;

public class PropertiesTask extends Task {
	private String host;
	private String hostip;

	private String port;
	private String sslport;
	private String contextpath;

	private String staticpath;

	private String cashost;
	private String casport;
	private String cassslport;
	private String cascontextpath;

	private String imagedir;
	private String ruledir;
	private String ruleindexdir;	
	
	private String nmdb;
	private String nmdbuser;
	private String nmdbpassword;
	private String nmdbhost;
	
	private String idtdb;
	private String idtdbuser;
	private String idtdbpassword;
	private String idtdbhost;
	

	@Override
	public void execute() {
		
		
		try {
			Properties app = new Properties();
			app.put("cas.server", getCasServerContext());
			app.put("cas.loginurl", getCasServerContext() + "login");
			app.put("cas.logouturl", getCasServerContext() + "logout");
			app.put("host.name", getHost());
			app.put("imageDir", getImagedir());
			app.put("mongo.host", getHostip());
			app.put("pdfServer", getHostip());
			app.put("sbase", getStaticpath());
			app.put("myappbase", getMyappbase());
			app.put("logouturl", getLogouturl());
			app.put("rulesDir", getRuledir());
			app.put("rulesIndexDir", getRuleindexdir());			
			app.put("mail.host", getHostip());
			app.put("rootUrl", getRootUrl());
			File basedir = getProject().getBaseDir();
			File target = new File(basedir, "target/classes");
			File myapp = new File(target, "myapp.properties");
			app.store(new FileOutputStream(myapp), "extention file");
			
			app = new Properties();
			app.put("user", getNmdbuser());
			app.put("password", getNmdbpassword());
			app.put("url", String.format("jdbc:mysql://%s/%s?useUnicode=true&characterEncoding=UTF-8"
					, getNmdbhost(),getNmdb()));
            myapp = new File(target, "jdbc-nm.properties");
			app.store(new FileOutputStream(myapp), "normal jdbc xa datasource properties");
		
			app = new Properties();
			app.put("user", getIdtdbuser());
			app.put("password", getIdtdbpassword());
			app.put("url", String.format("jdbc:mysql://%s/%s?useUnicode=true&characterEncoding=UTF-8"
					, getIdtdbhost(),getIdtdb()));
            myapp = new File(target, "jdbc-idt.properties");
			app.store(new FileOutputStream(myapp), "identified jdbc xa datasource properties");
		
		
		} catch (Exception e) {
			e.printStackTrace();
		}

	}



	public String getRootUrl() {
		return String.format("https://%s:%s/%s", getHost(), getSslport(),
				getContextpath());
	}

		
	public String getLogouturl() {
		return String.format("https://%s:%s/%s/logout", getCashost(),
				getCassslport(), getCascontextpath());
	}

	public String getMyappbase() {
		return String.format("https://%s:8443/myapps/", getHost(), getSslport());
	}

	public String getCasServerContext() {
		return String.format("https://%s:%s/%s/", getCashost(),
				getCassslport(), getCascontextpath());
	}

	public String getHost() {
		return host;
	}

	public void setHost(String host) {
		this.host = host;
	}

	public String getPort() {
		return port;
	}

	public void setPort(String port) {
		this.port = port;
	}

	public String getSslport() {
		return sslport;
	}

	public void setSslport(String sslport) {
		this.sslport = sslport;
	}

	public String getContextpath() {
		return contextpath;
	}

	public void setContextpath(String contextpath) {
		this.contextpath = contextpath;
	}

	public String getCasport() {
		return casport;
	}

	public void setCasport(String casport) {
		this.casport = casport;
	}

	public String getCassslport() {
		return cassslport;
	}

	public void setCassslport(String cassslport) {
		this.cassslport = cassslport;
	}

	public String getCascontextpath() {
		return cascontextpath;
	}

	public void setCascontextpath(String cascontextpath) {
		this.cascontextpath = cascontextpath;
	}

	public String getCashost() {
		return cashost;
	}

	public void setCashost(String cashost) {
		this.cashost = cashost;
	}

	public String getHostip() {
		return hostip;
	}

	public void setHostip(String hostip) {
		this.hostip = hostip;
	}

	public String getStaticpath() {
		return staticpath;
	}

	public void setStaticpath(String staticpath) {
		this.staticpath = staticpath;
	}

	public String getRuledir() {
		return ruledir;
	}

	public void setRuledir(String ruledir) {
		this.ruledir = ruledir;
	}

	public String getRuleindexdir() {
		return ruleindexdir;
	}

	public void setRuleindexdir(String ruleindexdir) {
		this.ruleindexdir = ruleindexdir;
	}


	public String getImagedir() {
		return imagedir;
	}

	public void setImagedir(String imagedir) {
		this.imagedir = imagedir;
	}



	public String getNmdb() {
		return nmdb;
	}



	public String getNmdbuser() {
		return nmdbuser;
	}



	public String getNmdbpassword() {
		return nmdbpassword;
	}



	public String getNmdbhost() {
		return nmdbhost;
	}



	public String getIdtdb() {
		return idtdb;
	}



	public String getIdtdbuser() {
		return idtdbuser;
	}



	public String getIdtdbpassword() {
		return idtdbpassword;
	}



	public String getIdtdbhost() {
		return idtdbhost;
	}



	public void setNmdb(String nmdb) {
		this.nmdb = nmdb;
	}



	public void setNmdbuser(String nmdbuser) {
		this.nmdbuser = nmdbuser;
	}



	public void setNmdbpassword(String nmdbpassword) {
		this.nmdbpassword = nmdbpassword;
	}



	public void setNmdbhost(String nmdbhost) {
		this.nmdbhost = nmdbhost;
	}



	public void setIdtdb(String idtdb) {
		this.idtdb = idtdb;
	}



	public void setIdtdbuser(String idtdbuser) {
		this.idtdbuser = idtdbuser;
	}



	public void setIdtdbpassword(String idtdbpassword) {
		this.idtdbpassword = idtdbpassword;
	}



	public void setIdtdbhost(String idtdbhost) {
		this.idtdbhost = idtdbhost;
	}
	
}
